﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace GeoFly
{
    public partial class WgenForm : Form
    {
        public WgenForm()
        {
            InitializeComponent();
        }
        public ClimateGene WGen = new ClimateGene();

        private void WgenForm_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string dir = "D:\\大气所\\刘永和找出的日气象数据\\54923-蒙阴\\";
            WGen.ReadMaxTempData(dir + "DAY54923.TMX");
            WGen.ReadMeanTempData(dir + "DAY54923.TA");
            WGen.ReadRainfallData(dir + "DAY54923.R");
            WGen.ReadRelaHumid(dir + "DAY54923.ua");
            WGen.ReadWindSpeed(dir + "DAY54923.wv");
            WGen.PrepareAllDewPoint();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            WGen.CalcMonthStat();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            double[,] Precipitation = new double[12, 31];
            bool IsDryPreDay = true;
            for (int month = 0; month < 12; month++)
            {
                for (int day = 0; day < WGen.daysInMonth[month]; day++)
                {
                    DateTime date = new DateTime(2008, month + 1, day + 1);
                    Precipitation[month, day] = WGen.GenPcp_Skewed(date, IsDryPreDay);
                    if (Precipitation[month, day] > 0.1)
                        IsDryPreDay = true;
                    else
                        IsDryPreDay = false;
                }
            }
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.DefaultExt = ".txt";
            dlg.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            dlg.Title = "选择存储位置";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                StreamWriter sw = new StreamWriter(dlg.FileName);
                for (int month = 0; month < 12; month++)
                {
                    sw.Write("month_" + (month + 1) + "：\t");
                    for (int day = 0; day < WGen.daysInMonth[month]; day++)
                    {
                        sw.Write(Math.Round(Precipitation[month, day], 2) + "\t");
                    }
                    sw.WriteLine();
                }
                sw.Close();
            }
        }

        private void button11_Click(object sender, EventArgs e)
        {
            List<double>[] result = new List<double>[12];
            for (int i = 0; i < 12; i++)
            {
                result[i] = new List<double>();
            }
            //将各月降雨量抽取出来
            foreach (int year in WGen.WeatherData.Keys)
            {
                for (int month = 0; month < 12; month++)
                {
                    for (int day = 0; day < WGen.daysInMonth[month]; day++)
                    {
                        if (WGen.WeatherData[year].Precipitation[month, day] > 0)
                            result[month].Add(WGen.WeatherData[year].Precipitation[month, day]);
                    }
                }
            }
            //保存

            SaveFileDialog dlg = new SaveFileDialog();
            dlg.DefaultExt = ".txt";
            dlg.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            dlg.Title = "选择存储位置";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                StreamWriter sw = new StreamWriter(dlg.FileName);
                for (int month = 0; month < 12; month++)
                {
                    sw.Write("month_" + (month + 1) + "：\t");
                    foreach (double value in result[month])
                    {
                        sw.Write(value + "\t");
                    }
                    sw.WriteLine();
                }
                sw.Close();
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            openFileDialog1.DefaultExt = ".R";
            openFileDialog1.Filter = "降雨量文件(*.R)|*.R|所有文件(*.*)|*.*";
            openFileDialog1.Title = "选择单站点多年降水数据的存储位置";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                WGen.ReadRainfallData(openFileDialog1.FileName);
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            openFileDialog1.DefaultExt = ".tmx";
            openFileDialog1.Filter = "日最高气温文件(*.tmx)|*.tmx|所有文件(*.*)|*.*";
            openFileDialog1.Title = "选择单站点多年日最高气温数据的存储位置";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                WGen.ReadMaxTempData(openFileDialog1.FileName);
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            openFileDialog1.DefaultExt = ".ta";
            openFileDialog1.Filter = "日平均气温文件(*.ta)|*.ta|所有文件(*.*)|*.*";
            openFileDialog1.Title = "选择单站点多年日平均气温数据的存储位置";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                WGen.ReadMeanTempData(openFileDialog1.FileName);
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            openFileDialog1.DefaultExt = ".wv";
            openFileDialog1.Filter = "日平均风速文件(*.wv)|*.wv|所有文件(*.*)|*.*";
            openFileDialog1.Title = "选择单站点多年日平均风速数据的存储位置";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                WGen.ReadWindSpeed(openFileDialog1.FileName);
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            openFileDialog1.DefaultExt = ".ua";
            openFileDialog1.Filter = "日平均风速文件(*.ua)|*.ua|所有文件(*.*)|*.*";
            openFileDialog1.Title = "选择单站点多年日平均相对湿度数据的存储位置";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                WGen.ReadRelaHumid(openFileDialog1.FileName);
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            WGen.PrepareAllDewPoint();
        }
    }
}
